package com.binhthuan.catechist.client.grid;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.component.PopupDialog;
import com.binhthuan.catechist.client.data.CourseDS;
import com.binhthuan.catechist.client.data.GradeDS;
import com.binhthuan.catechist.client.data.ShiftDS;
import com.binhthuan.catechist.client.data.TrainerDS;
import com.binhthuan.catechist.client.data.VenueDS;
import com.binhthuan.catechist.client.viewer.TrainerViewer;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;

public class CourseGrid extends AbstractGrid {

    public CourseGrid() {
    }

    public CourseGrid(CourseDS dataSource) {
        super(dataSource);
        init(this);
        setWrapCells(true);
        setFixedRecordHeights(false);

        ListGridField idField = new ListGridField(Parameter.ID);
        idField.setHidden(true);
        idField.setCanHide(false);

        ListGridField code = new ListGridField(Parameter.CODE);
        code.setTitle(ECApplication.getConstants().courseCode());

        ListGridField name = new ListGridField(Parameter.NAME);
        name.setTitle(ECApplication.getConstants().name());

        ListGridField grade = new ListGridField(Parameter.GRADE);
        grade.setTitle(ECApplication.getConstants().grade());
        grade.setOptionDataSource(GradeDS.getInstance());
        grade.setDisplayField(Parameter.NAME);
        grade.setAutoFetchDisplayMap(true);
        grade.setValueField(Parameter.ID);

        ListGridField shift = new ListGridField(Parameter.SHIFT);
        shift.setTitle(ECApplication.getConstants().shift());
        shift.setOptionDataSource(ShiftDS.getInstance());
        shift.setDisplayField(Parameter.NAME);
        shift.setValueField(Parameter.ID);

        ListGridField venue = new ListGridField(Parameter.VENUE);
        venue.setTitle(ECApplication.getConstants().venue());
        venue.setOptionDataSource(VenueDS.getInstance());
        venue.setDisplayField(Parameter.NAME);
        venue.setValueField(Parameter.ID);
/*
        ListGridField trainers = new ListGridField(Parameter.TRAINERS);
        trainers.setTitle(ECApplication.getConstants().trainer());
        trainers.setOptionDataSource(TrainerDS.getInstance());
        trainers.setDisplayField(Parameter.FULL_NAME);
        trainers.setValueField(Parameter.ID);
        trainers.setMultiple(true);

        trainers.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                //Record trainers[] = record.getAttributeAsRecordArray(Parameter.TRAINERS);
                StringBuilder builder = new StringBuilder();
                //for (Record t : trainers) {
                //    builder.append("<a href=\"javascript:showTrainerDetail('" + t.getAttributeAsString(Parameter.ID)
                //            + "')\">" + t.getAttributeAsString(Parameter.FIRST_NAME) + "</a>");
                //    builder.append("<br/>");
                //}
                //builder.delete(builder.lastIndexOf("<br/>"), builder.length());
                String trainerIds = record.getAttribute(Parameter.TRAINERS);
                String ids[] = trainers.split(",");
                for(String id:ids) {
                    System.out.println(i + " --- " + record.getAttribute(i));
                    builder.append(i);
                }
                return builder.toString();
            }
        });
*/
        setSortField(Parameter.GRADE);
        setFields(idField, code, name, grade, shift, venue);
        setAutoFetchData(false);
    }

    private native void init(CourseGrid grid) /*-{
        $wnd.showTrainerDetail = function (id) {
           grid.@com.binhthuan.catechist.client.grid.CourseGrid::showTrainerDetail(Ljava/lang/String;)(id);
        };
    }-*/;

    public void showTrainerDetail(String id) {
        TrainerViewer viewer = new TrainerViewer(TrainerDS.getInstance());
        viewer.fetchData(new Criteria(Parameter.ID, id));
        PopupDialog dialog = new PopupDialog(true);
        dialog.addItem(viewer.createCanvas());
        dialog.show();
    }
}
